package com.ubing.gxspringboot.gxproxy;

import com.ubing.gxspringboot.gxaop.RequestAspect;
import com.ubing.gxspringboot.gxservice.TestService;
import com.ubing.gxspringboot.gxservice.gxserviceimpl.TestServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AOPProxy implements InvocationHandler {

    private Object target;
    private String methodName;
    private Class<?> clazz;
    public Object bind(Object target,String methodName,Class<?> clazz) {
        this.target = target;
        this.methodName=methodName;
        this.clazz=clazz;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        AOPJoinPoint joinPoint = new AOPJoinPoint(target, method, args);
        return clazz.getDeclaredMethod(methodName, JoinPoint.class).invoke(clazz.newInstance(), joinPoint);
    }
}
